package createThread.cn.tedu.threaddemo.test8.单例模式;

/**
 * 1.饿汉式
 */
class SingleHungryTest {
    private static SingleHungryTest instance = new SingleHungryTest();
    private SingleHungryTest(){
    }
    public static SingleHungryTest getInstance(){
        return instance;
    }
}

/**
 * 2.懒汉式
 */
class SingleLazyTest {
    private static SingleLazyTest instance = null;

    private SingleLazyTest() {
    }
    private static final Object lock = new Object();
    public static SingleLazyTest getInstance(){
        if(instance == null){
            synchronized (lock){
                if(instance == null){
                    instance = new SingleLazyTest();
                }
            }
        }
        return instance;
    }
}

public class Demo1 {
    public static void main(String[] args) {
        SingleHungryTest hungryTest = SingleHungryTest.getInstance();
        SingleLazyTest lazyTest = SingleLazyTest.getInstance();
        Thread t1 = new Thread(()->{

        });
    }
}
